package com.teamcenter.rac.massupdate.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.eclipse.ui.views.properties.IPropertySource;

import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentChangeItemRevision;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.massupdate.model.ImpactedPart;
import com.teamcenter.rac.massupdate.model.MassUpdateChangeObjectValidationModel;
import com.teamcenter.rac.massupdate.model.UpdateJobModel;
import com.teamcenter.rac.massupdate.model.UpdateResult;
import com.teamcenter.rac.massupdate.ui.Messages;
import com.teamcenter.rac.util.AdapterUtil;
import com.teamcenter.services.loose.structuremanagement.MassUpdateService;
import com.teamcenter.services.loose.structuremanagement._2012_09.MassUpdate;
import com.teamcenter.services.loose.structuremanagement._2014_10.MassUpdate.ValidateChangeObjectForMassUpdateOutput;
import com.teamcenter.services.loose.structuremanagement._2014_10.MassUpdate.ValidateChangeObjectForMassUpdateResponse;
import com.teamcenter.soa.client.model.ModelObject;
@SuppressWarnings({"rawtypes","unchecked"})
public class MassUpdateSupportOperation implements IMassUpdateSupportOperation, IMassUpdateMarkupSupportOperation {
	public List<Object> executeOnePass(UpdateJobModel paramUpdateJobModel, List<ImpactedPart> paramList,
			TCSession paramTCSession) {
		if (paramUpdateJobModel == null) {
			return null;
		}
		MassUpdate.MassUpdateExecuteECRinput[] arrayOfMassUpdateExecuteECRinput = new MassUpdate.MassUpdateExecuteECRinput[1];
		arrayOfMassUpdateExecuteECRinput[0] = createExecute1Input(1, paramUpdateJobModel, paramList, paramTCSession);
		return performExecute1(arrayOfMassUpdateExecuteECRinput, paramTCSession);
	}

	protected MassUpdate.MassUpdateExecuteECRinput createExecute1Input(int paramInt, UpdateJobModel paramUpdateJobModel,
			List<ImpactedPart> paramList, TCSession paramTCSession) {
		if ((paramUpdateJobModel == null) || (paramInt == 3)) {
			return null;
		}
		TCComponent localTCComponent1 = null;
		if (paramUpdateJobModel.getTargetPart() != null) {
			localTCComponent1 = SourceHelper.getAssociatedComponent(paramUpdateJobModel.getTargetPart(),
					paramTCSession);
		}
		TCComponent localTCComponent2 = null;
		if (paramUpdateJobModel.getChangeObject() != null) {
			localTCComponent2 = SourceHelper.getAssociatedComponent(paramUpdateJobModel.getChangeObject(),
					paramTCSession);
		}
		TCComponent localTCComponent3 = null;
		if (paramUpdateJobModel.getReplacementPart() != null) {
			localTCComponent3 = SourceHelper.getAssociatedComponent(paramUpdateJobModel.getReplacementPart(),
					paramTCSession);
		}
		int i = paramList.size();
		ArrayList localArrayList1 = new ArrayList(i);
		ArrayList localArrayList2 = new ArrayList(i);
		Iterator localIterator1 = paramList.iterator();
		while (localIterator1.hasNext()) {
			Object localObject1 = localIterator1.next();
			Object localObject2 = (ImpactedPart) localObject1;
			if (localObject2 != null) {
				localArrayList1.add((TCComponent) AdapterUtil.getAdapter(((ImpactedPart) localObject2).getImpacted(),
						TCComponent.class));
				localArrayList2.add(Boolean.valueOf(((ImpactedPart) localObject2).isSelected()));
			}
		}
		boolean[] localObject1 = new boolean[localArrayList2.size()];
		int j = 0;
		Iterator<Boolean> localIterator2 = localArrayList2.iterator();
		while (localIterator2.hasNext()) {
			Boolean localObject2 = (Boolean) localIterator2.next();
			localObject1[(j++)] = ((Boolean) localObject2).booleanValue();
		}
		Object localObject2 = new MassUpdate.MassUpdateExecuteECRinput();
		if (paramInt == 1) {
			((MassUpdate.MassUpdateExecuteECRinput) localObject2).executionMode = 1;
		} else if (paramInt == 2) {
			((MassUpdate.MassUpdateExecuteECRinput) localObject2).executionMode = 2;
		}
		((MassUpdate.MassUpdateExecuteECRinput) localObject2).operation = paramUpdateJobModel.getOperationType()
				.convertToInt();
		((MassUpdate.MassUpdateExecuteECRinput) localObject2).target = localTCComponent1;
		((MassUpdate.MassUpdateExecuteECRinput) localObject2).newItem = localTCComponent3;
		((MassUpdate.MassUpdateExecuteECRinput) localObject2).change = localTCComponent2;
		((MassUpdate.MassUpdateExecuteECRinput) localObject2).parents = ((ModelObject[]) localArrayList1
				.toArray(new TCComponent[localArrayList1.size()]));
		((MassUpdate.MassUpdateExecuteECRinput) localObject2).sParents = ((boolean[]) localObject1);
		((MassUpdate.MassUpdateExecuteECRinput) localObject2).addToProblem = paramUpdateJobModel
				.isSaveTargetPartInFolder();
		((MassUpdate.MassUpdateExecuteECRinput) localObject2).addToSolution = paramUpdateJobModel
				.isSaveReplacementPartInFolder();
		return (MassUpdate.MassUpdateExecuteECRinput) localObject2;
	}

	public List<Object> executeMarkup(TCComponentItemRevision paramTCComponentItemRevision) {
		if (paramTCComponentItemRevision == null) {
			return null;
		}
		ArrayList localArrayList = new ArrayList();
		try {
			MassUpdateService localObject = MassUpdateService
					.getService(paramTCComponentItemRevision.getSession().getSoaConnection());
			MassUpdate.MassUpdateExecuteECNresponse localMassUpdateExecuteECNresponse = ((MassUpdateService) localObject)
					.massUpdateExecutionECN(new TCComponent[] { paramTCComponentItemRevision });
			if (localMassUpdateExecuteECNresponse != null) {
				int k;
				if (localMassUpdateExecuteECNresponse.outputs != null) {
					MassUpdate.MassUpdateExecuteECNoutput[] arrayOfMassUpdateExecuteECNoutput;
					k = (arrayOfMassUpdateExecuteECNoutput = localMassUpdateExecuteECNresponse.outputs).length;
					for (int j = 0; j < k; j++) {
						MassUpdate.MassUpdateExecuteECNoutput localMassUpdateExecuteECNoutput = arrayOfMassUpdateExecuteECNoutput[j];
						localArrayList.addAll(Arrays.asList(localMassUpdateExecuteECNoutput.markupChanges));
					}
				}
				if (localMassUpdateExecuteECNresponse.serviceData != null) {
					for (int i = 0; i < localMassUpdateExecuteECNresponse.serviceData.sizeOfPartialErrors(); i++) {
						String[] arrayOfString;
						int m = (arrayOfString = localMassUpdateExecuteECNresponse.serviceData.getPartialError(i)
								.getMessages()).length;
						for (k = 0; k < m; k++) {
							String str = arrayOfString[k];
							localArrayList.add(new TCException(str));
						}
					}
				}
			}
		} catch (Exception localException) {
			Object localObject = new Exception(Messages.Exception_SOAFailed);
			localArrayList.add(localObject);
		}
		return localArrayList;
	}

	public List<Object> executeSaveMarkup(UpdateJobModel paramUpdateJobModel, List<ImpactedPart> paramList,
			TCSession paramTCSession) {
		if (paramUpdateJobModel == null) {
			return null;
		}
		MassUpdate.MassUpdateExecuteECRinput[] arrayOfMassUpdateExecuteECRinput = new MassUpdate.MassUpdateExecuteECRinput[1];
		arrayOfMassUpdateExecuteECRinput[0] = createExecute1Input(2, paramUpdateJobModel, paramList, paramTCSession);
		return performExecute1(arrayOfMassUpdateExecuteECRinput, paramTCSession);
	}

	public boolean isAllowedECNType(TCComponentItemRevision paramTCComponentItemRevision) {
		if (paramTCComponentItemRevision == null) {
			return false;
		}
		TCPreferenceService localTCPreferenceService = paramTCComponentItemRevision.getSession().getPreferenceService();
		if (localTCPreferenceService == null) {
			return false;
		}
		String[] arrayOfString1 = localTCPreferenceService.getStringValues("CM_massupdate_allowed_ECN_types");
		if ((arrayOfString1 == null) || (arrayOfString1.length == 0)) {
			return true;
		}
		String[] arrayOfString2;
		int j = (arrayOfString2 = arrayOfString1).length;
		for (int i = 0; i < j; i++) {
			String str = arrayOfString2[i];
			if (str.equals(paramTCComponentItemRevision.getType())) {
				return true;
			}
		}
		return false;
	}

	public boolean isAllowedECRType(TCComponentItemRevision paramTCComponentItemRevision) {
		if (paramTCComponentItemRevision == null) {
			return false;
		}
		TCPreferenceService localTCPreferenceService = paramTCComponentItemRevision.getSession().getPreferenceService();
		if (localTCPreferenceService == null) {
			return false;
		}
		String[] arrayOfString1 = localTCPreferenceService.getStringValues("CM_massupdate_allowed_ECR_types");
		if ((arrayOfString1 == null) || (arrayOfString1.length == 0)) {
			return true;
		}
		String[] arrayOfString2;
		int j = (arrayOfString2 = arrayOfString1).length;
		for (int i = 0; i < j; i++) {
			String str = arrayOfString2[i];
			if (str.equals(paramTCComponentItemRevision.getType())) {
				return true;
			}
		}
		return false;
	}

	public boolean isChangeSupported(TCSession paramTCSession) {
		Boolean localBoolean = paramTCSession.getPreferenceService().getLogicalValue("CM_Massupdate_Support_Change");
		if (localBoolean == null) {
			return false;
		}
		return localBoolean.booleanValue();
	}

	public List<Object> performExecute1(MassUpdate.MassUpdateExecuteECRinput[] paramArrayOfMassUpdateExecuteECRinput,
			TCSession paramTCSession) {
		if ((paramArrayOfMassUpdateExecuteECRinput == null) || (paramArrayOfMassUpdateExecuteECRinput.length == 0)) {
			return null;
		}
		ArrayList localArrayList = new ArrayList();
		try {
			Object localObject1 = MassUpdateService.getService(paramTCSession.getSoaConnection());
			MassUpdate.MassUpdateExecuteECRresponse localMassUpdateExecuteECRresponse = ((MassUpdateService) localObject1)
					.massUpdateExecutionECR(paramArrayOfMassUpdateExecuteECRinput);
			if (localMassUpdateExecuteECRresponse != null) {
				int k;
				String[] localObject2;
				if (localMassUpdateExecuteECRresponse.outputs != null) {
					MassUpdate.MassUpdateExecuteECRoutput[] arrayOfMassUpdateExecuteECRoutput;
					k = (arrayOfMassUpdateExecuteECRoutput = localMassUpdateExecuteECRresponse.outputs).length;
					for (int j = 0; j < k; j++) {
						MassUpdate.MassUpdateExecuteECRoutput localMassUpdateExecuteECRoutput = arrayOfMassUpdateExecuteECRoutput[j];
						IPropertySource _localObject2 = (IPropertySource) AdapterUtil
								.getAdapter(localMassUpdateExecuteECRoutput.parent, IPropertySource.class);
						UpdateResult localUpdateResult = new UpdateResult();
						localUpdateResult.setImpactedPart((IPropertySource) _localObject2);
						localUpdateResult.setResultComment(localMassUpdateExecuteECRoutput.statusComment);
						localUpdateResult.setResultStatus(localMassUpdateExecuteECRoutput.status);
						localArrayList.add(localUpdateResult);
					}
				}
				if (localMassUpdateExecuteECRresponse.serviceData != null) {
					for (int i = 0; i < localMassUpdateExecuteECRresponse.serviceData.sizeOfPartialErrors(); i++) {
						int m = (localObject2 = localMassUpdateExecuteECRresponse.serviceData.getPartialError(i)
								.getMessages()).length;
						for (k = 0; k < m; k++) {
							String str = localObject2[k];
							localArrayList.add(new TCException(str));
						}
					}
				}
			}
		} catch (Exception localException) {
			Object localObject1 = new Exception(Messages.Exception_SOAFailed);
			localArrayList.add(localObject1);
		}
		return localArrayList;
	}

	public List<Object> executeRemoveMarkupChange(TCComponentItemRevision paramTCComponentItemRevision,
			List<TCComponent> paramList, TCSession paramTCSession) {
		if ((paramTCComponentItemRevision == null) || (paramList == null) || (paramList.isEmpty())) {
			return null;
		}
		MassUpdate.MassUpdateExecuteECRinput[] arrayOfMassUpdateExecuteECRinput = new MassUpdate.MassUpdateExecuteECRinput[paramList
				.size()];
		for (int i = 0; i < paramList.size(); i++) {
			arrayOfMassUpdateExecuteECRinput[i] = new MassUpdate.MassUpdateExecuteECRinput();
			arrayOfMassUpdateExecuteECRinput[i].executionMode = 3;
			arrayOfMassUpdateExecuteECRinput[i].change = paramTCComponentItemRevision;
			arrayOfMassUpdateExecuteECRinput[i].markupChange = ((ModelObject) paramList.get(i));
		}
		return performExecute1(arrayOfMassUpdateExecuteECRinput, paramTCSession);
	}

	public void validateChangeObjects(MassUpdateChangeObjectValidationModel paramMassUpdateChangeObjectValidationModel,
			TCSession paramTCSession) {
		MassUpdateService localMassUpdateService = MassUpdateService.getService(paramTCSession.getSoaConnection());
		ValidateChangeObjectForMassUpdateResponse localValidateChangeObjectForMassUpdateResponse = localMassUpdateService
				.validateChangeObjectForMassUpdate(paramMassUpdateChangeObjectValidationModel.getChangeItemRevisions(),
						paramMassUpdateChangeObjectValidationModel.getMassUpdateProcessType());
		ValidateChangeObjectForMassUpdateOutput[] arrayOfValidateChangeObjectForMassUpdateOutput;
		int k;
		if (localValidateChangeObjectForMassUpdateResponse.output != null) {
			k = (arrayOfValidateChangeObjectForMassUpdateOutput = localValidateChangeObjectForMassUpdateResponse.output).length;
			for (int j = 0; j < k; j++) {
				ValidateChangeObjectForMassUpdateOutput localValidateChangeObjectForMassUpdateOutput = arrayOfValidateChangeObjectForMassUpdateOutput[j];
				paramMassUpdateChangeObjectValidationModel.setChangeItemRevisionValidity(
						(TCComponentChangeItemRevision) AdapterUtil.getAdapter(
								localValidateChangeObjectForMassUpdateOutput.changeItemRev,
								TCComponentChangeItemRevision.class),
						Boolean.valueOf(localValidateChangeObjectForMassUpdateOutput.isValid));
			}
		}
		if (localValidateChangeObjectForMassUpdateResponse.serviceData != null) {
			int i = localValidateChangeObjectForMassUpdateResponse.serviceData.sizeOfPartialErrors();
			if (i > 0) {
				TCException[] arrayOfTCException = new TCException[i];
				for (k = 0; k < i; k++) {
					String[] arrayOfString;
					int n = (arrayOfString = localValidateChangeObjectForMassUpdateResponse.serviceData
							.getPartialError(k).getMessages()).length;
					for (int m = 0; m < n; m++) {
						String msg = arrayOfString[m];
						arrayOfTCException[k] = new TCException(msg);
					}
				}
				paramMassUpdateChangeObjectValidationModel.setErrorMessages(arrayOfTCException);
			}
		}
	}
}
